home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / specfunc / beta_inc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  5.1 KB  |  181 lines

  1. /* specfunc/beta_inc.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman */
  21.  
  22. #include <config.h>
  23. #include <gsl/gsl_math.h>
  24. #include <gsl/gsl_errno.h>
  25. #include <gsl/gsl_sf_log.h>
  26. #include <gsl/gsl_sf_exp.h>
  27. #include <gsl/gsl_sf_gamma.h>
  28.  
  29. #include "error.h"
  30. #include "check.h"
  31.  
  32. static
  33. int
  34. beta_cont_frac(
  35.   const double a,
  36.   const double b,
  37.   const double x,
  38.   gsl_sf_result * result
  39.   )
  40. {
  41.   const unsigned int max_iter = 512;        /* control iterations      */
  42.   const double cutoff = 2.0 * GSL_DBL_MIN;  /* control the zero cutoff */
  43.   unsigned int iter_count = 0;
  44.   double cf;
  45.  
  46.   /* standard initialization for continued fraction */
  47.   double num_term = 1.0;
  48.   double den_term = 1.0 - (a+b)*x/(a+1.0);
  49.   if (fabs(den_term) < cutoff) den_term = cutoff;
  50.   den_term = 1.0/den_term;
  51.   cf = den_term;
  52.  
  53.   while(iter_count < max_iter) {
  54.     const int k  = iter_count + 1;
  55.     double coeff = k*(b-k)*x/(((a-1.0)+2*k)*(a+2*k));
  56.     double delta_frac;
  57.  
  58.     /* first step */
  59.     den_term = 1.0 + coeff*den_term;
  60.     num_term = 1.0 + coeff/num_term;
  61.     if(fabs(den_term) < cutoff) den_term = cutoff;
  62.     if(fabs(num_term) < cutoff) num_term = cutoff;
  63.     den_term  = 1.0/den_term;
  64.  
  65.     delta_frac = den_term * num_term;
  66.     cf *= delta_frac;
  67.  
  68.     coeff = -(a+k)*(a+b+k)*x/((a+2*k)*(a+2*k+1.0));
  69.  
  70.     /* second step */
  71.     den_term = 1.0 + coeff*den_term;
  72.     num_term = 1.0 + coeff/num_term;
  73.     if(fabs(den_term) < cutoff) den_term = cutoff;
  74.     if(fabs(num_term) < cutoff) num_term = cutoff;
  75.     den_term = 1.0/den_term;
  76.  
  77.     delta_frac = den_term*num_term;
  78.     cf *= delta_frac;
  79.  
  80.     if(fabs(delta_frac-1.0) < 2.0*GSL_DBL_EPSILON) break;
  81.  
  82.     ++iter_count;
  83.   }
  84.  
  85.   result->val = cf;
  86.   result->err = iter_count * 4.0 * GSL_DBL_EPSILON * fabs(cf);
  87.  
  88.   if(iter_count >= max_iter)
  89.     GSL_ERROR ("error", GSL_EMAXITER);
  90.   else
  91.     return GSL_SUCCESS;
  92. }
  93.  
  94.  
  95.  
  96. /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
  97.  
  98. int
  99. gsl_sf_beta_inc_e(
  100.   const double a,
  101.   const double b,
  102.   const double x,
  103.   gsl_sf_result * result
  104.   )
  105. {
  106.   if(a <= 0.0 || b <= 0.0 || x < 0.0 || x > 1.0) {
  107.     DOMAIN_ERROR(result);
  108.   }
  109.   else if(x == 0.0) {
  110.     result->val = 0.0;
  111.     result->err = 0.0;
  112.     return GSL_SUCCESS;
  113.   }
  114.   else if(x == 1.0) {
  115.     result->val = 1.0;
  116.     result->err = 0.0;
  117.     return GSL_SUCCESS;
  118.   }
  119.   else {
  120.     gsl_sf_result ln_beta;
  121.     gsl_sf_result ln_x;
  122.     gsl_sf_result ln_1mx;
  123.     gsl_sf_result prefactor;
  124.     const int stat_ln_beta = gsl_sf_lnbeta_e(a, b, &ln_beta);
  125.     const int stat_ln_1mx = gsl_sf_log_1plusx_e(-x, &ln_1mx);
  126.     const int stat_ln_x = gsl_sf_log_e(x, &ln_x);
  127.     const int stat_ln = GSL_ERROR_SELECT_3(stat_ln_beta, stat_ln_1mx, stat_ln_x);
  128.  
  129.     const double ln_pre_val = -ln_beta.val + a * ln_x.val + b * ln_1mx.val;
  130.     const double ln_pre_err =  ln_beta.err + fabs(a*ln_x.err) + fabs(b*ln_1mx.err);
  131.     const int stat_exp = gsl_sf_exp_err_e(ln_pre_val, ln_pre_err, &prefactor);
  132.  
  133.     if(stat_ln != GSL_SUCCESS) {
  134.       result->val = 0.0;
  135.       result->err = 0.0;
  136.       GSL_ERROR ("error", GSL_ESANITY);
  137.     }
  138.  
  139.     if(x < (a + 1.0)/(a+b+2.0)) {
  140.       /* Apply continued fraction directly. */
  141.       gsl_sf_result cf;
  142.       const int stat_cf = beta_cont_frac(a, b, x, &cf);
  143.       int stat;
  144.       result->val = prefactor.val * cf.val / a;
  145.       result->err = (fabs(prefactor.err * cf.val) + fabs(prefactor.val * cf.err))/a;
  146.  
  147.       stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
  148.       if(stat == GSL_SUCCESS) {
  149.     CHECK_UNDERFLOW(result);
  150.       }
  151.       return stat;
  152.     }
  153.     else {
  154.       /* Apply continued fraction after hypergeometric transformation. */
  155.       gsl_sf_result cf;
  156.       const int stat_cf = beta_cont_frac(b, a, 1.0-x, &cf);
  157.       int stat;
  158.       const double term = prefactor.val * cf.val / b;
  159.       result->val  = 1.0 - term;
  160.       result->err  = fabs(prefactor.err * cf.val)/b;
  161.       result->err += fabs(prefactor.val * cf.err)/b;
  162.       result->err += 2.0 * GSL_DBL_EPSILON * (1.0 + fabs(term));
  163.       stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
  164.       if(stat == GSL_SUCCESS) {
  165.     CHECK_UNDERFLOW(result);
  166.       }
  167.       return stat;
  168.     }
  169.   }
  170. }
  171.  
  172.  
  173. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  174.  
  175. #include "eval.h"
  176.  
  177. double gsl_sf_beta_inc(const double a, const double b, const double x)
  178. {
  179.   EVAL_RESULT(gsl_sf_beta_inc_e(a, b, x, &result));
  180. }
  181.